package cn.wenhaha.app.data.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.util.StrUtil;
import cn.wenhaha.app.data.service.DataPluginService;
import cn.wenhaha.common.bean.dto.mapper.DataPluginInfoVoMapper;
import cn.wenhaha.common.bean.entity.Switch;
import cn.wenhaha.common.bean.vo.DataPluginInfoVo;
import cn.wenhaha.common.core.domain.AjaxV1Result;
import cn.wenhaha.common.db.bean.SysDataSource;
import cn.wenhaha.common.exception.ApiMSGException;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;


@RestController
@RequestMapping("/api/v1/data/plugin")
public class ApiDataPluginLibController {

    @Resource
    private DataPluginService dataPluginService;



    /**
     * 添加插件
     * @return
     */
    @PostMapping("add")
    @SaCheckRole("admin")
    public AjaxV1Result<String> add(@RequestParam("file") MultipartFile file) {
        return AjaxV1Result.newSuccess(null,dataPluginService.addPlugin(file));
    }


    /**
     * 升级或者降级
     * @param file
     * @return
     */
    @PostMapping("update")
    @SaCheckRole("admin")
    public AjaxV1Result<String> update(@RequestParam("file") MultipartFile file) {
        return AjaxV1Result.newSuccess(null,dataPluginService.update(file));
    }


    /**
     * 切换插件启用状态<br/>
     * 需检查上下文中，获取对象是否有空对象，如果有则不允许启用
     * @return
     */
    @GetMapping("switch")
    @SaCheckRole("admin")
    public AjaxV1Result<String> switchStatus(String id){
        SysDataSource sysDataSource = Optional.ofNullable(SysDataSource.dao.findById(id))
                .orElseThrow(() -> ApiMSGException.build(7009));
        String status = sysDataSource.getStatus();
        if (StrUtil.equals(status, Switch.ON.getCode().toString())){
            // 关掉
            dataPluginService.disabled(sysDataSource);
        } else {
            // 开启
            dataPluginService.enable(sysDataSource);
        }
        return AjaxV1Result.newSuccess();
    }


    /**
     * 插件列表
     * @return
     */
    @PostMapping("list")
    @SaCheckLogin
    public AjaxV1Result<?> list(){
        Page<SysDataSource> sysDataSourcePage = SysDataSource.dao.pageList();

        List<DataPluginInfoVo> dataPluginInfoVos = DataPluginInfoVoMapper.INSTANCE.toList(sysDataSourcePage.getList());
        Page<DataPluginInfoVo> dataPluginInfoVoPage = new Page<>(dataPluginInfoVos, sysDataSourcePage.getPageNumber(),
                sysDataSourcePage.getPageSize(), sysDataSourcePage.getTotalPage(), sysDataSourcePage.getTotalRow());
        return AjaxV1Result.newSuccess(dataPluginInfoVoPage);
    }

    /**
     * 查询插件详情
     * @return
     */
    @GetMapping("get")
    @SaCheckLogin
    public AjaxV1Result<?> get(String id){
        SysDataSource dataSource = SysDataSource.dao.findById(id);
        if (dataSource==null){
            return AjaxV1Result.newFail("数据为空");
        }
        return AjaxV1Result.newSuccess(DataPluginInfoVoMapper.INSTANCE.toVo(dataSource));
    }


    /**
     * 删除插件
     * @return
     */
    @GetMapping("remove")
    @SaCheckRole("admin")
    public AjaxV1Result<String> remove(String id){
        if (id!=null){
            dataPluginService.remove(id);
        }
        return AjaxV1Result.newSuccess();
    }




}
